﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYView;
using QuickModel.LastFastModeling;

namespace QuickModel.Command
{
	public class CommandHelper
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements, string speciatly, string equipType)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			CommandHelper.SetRevitWorkWindowFocused(activeUIDocument);
			CmdViewType types = (CmdViewType)15;
			bool flag = SwitchView.SetUsableView(types, commandData) > 0;
			Result result;
			if (flag)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			else
			{
				QuickModelFrameWork quickModelFrameWork = new QuickModelFrameWork(new LastFastModelingMaker(commandData)
				{
					Speciatly = speciatly,
					EquipType = equipType
				}, new LastFastModelingRespondHanlder());
				quickModelFrameWork.ExcuteWithWhile(commandData);
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			return result;
		}

		public static void SetRevitWorkWindowFocused(UIDocument uiDoc)
		{
			try
			{
				Thread.Sleep(200);
				Application.DoEvents();
			}
			catch
			{
			}
		}

		public static void ChangeFocus(ExternalCommandData commandData)
		{
			try
			{
				UIApplication application = commandData.Application;
				Autodesk.Revit.DB.View activeView = application.ActiveUIDocument.ActiveView;
				IList<UIView> openUIViews = application.ActiveUIDocument.GetOpenUIViews();
				foreach (UIView uiview in openUIViews)
				{
					bool flag = activeView == null || uiview.ViewId.IntegerValue != activeView.Id.IntegerValue;
					if (flag)
					{
						Autodesk.Revit.DB.View activeView2 = commandData.Application.ActiveUIDocument.Document.GetElement(uiview.ViewId) as Autodesk.Revit.DB.View;
						application.ActiveUIDocument.ActiveView = activeView2;
						application.ActiveUIDocument.ActiveView = activeView;
						return;
					}
				}
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(application.ActiveUIDocument.Document);
				FilteredElementIterator elementIterator = filteredElementCollector.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
				elementIterator.Reset();
				while (elementIterator.MoveNext())
				{
					Element element = elementIterator.Current;
					Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					bool flag2 = view == null || view.IsTemplate;
					if (!flag2)
					{
						bool flag3 = view.Id.IntegerValue == activeView.Id.IntegerValue;
						if (!flag3)
						{
							bool flag4 = (int)view.ViewType == 4 || (int)view.ViewType == 1 || (int)view.ViewType == 2 || (int)view.ViewType == 3 || (int)view.ViewType == 117;
							if (flag4)
							{
								application.ActiveUIDocument.ActiveView = view;
								application.ActiveUIDocument.ActiveView = activeView;
								break;
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}
	}
}
